package com.timothy.android.answer;

import com.timothy.android.R;
import com.timothy.android.view.Bar2DVerView;
import com.timothy.service.MessageSenderUtil;

import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

public class Bar2DVerActivity extends BaseChartActivity{
	private final static String LOG_TAG = "Bar2DVerActivity";

	Bar2DVerView bar2DVerView;
	private GestureDetector detector = null;
	
	//header component(left, center,right)
	TextView answerDesc;
//	TextView priorityList;
	Button nextBtn;
	
	@Override
	public void setHeader(Integer type) {
		//set answerDesc
		answerDesc = (TextView) this.findViewById(R.id.answerDesc);
		answerDesc.setText("Q " + baseBean.getQuestionIndex().toString()+" of " + baseBean.getTotalQuestionCount().toString());
		
		//set priorityList
		if("42".equals(type)){
			priorityList = (TextView) this.findViewById(R.id.priorityList);
			priorityList.setText("Priority Change");
			priorityList.setVisibility(0);
			priorityList.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					showWindow(v);
				}
			});
		}
		
		//set next button
		if(baseBean.getQuestionIndex() == baseBean.getTotalQuestionCount()){
			ShowDialog("Vote Ended ! Please visit www.powercomars.com for more product information.");
		}else{
			if (MessageSenderUtil.MODE != null && MessageSenderUtil.MODE == 2) {
				nextBtn = (Button) this.findViewById(R.id.nextBtn);
				nextBtn.setVisibility(View.VISIBLE);
				nextBtn.setOnClickListener(new Button.OnClickListener() {
					public void onClick(View v) {
						sendNextQuestion();
					}
				});
			}
		}

	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.answer_2d_ver_bar);
		
		//get displayStyle and set header
//		Bundle bundle = this.getIntent().getExtras();
//		String displayStyle = bundle.getString("displayStyle");
//		String type = bundle.getString("type");
//		setHeader(displayStyle,type);
		
		setHeader(baseBean.getQuestionType());

		//get bar2DVerView and set bar2DVerView
		bar2DVerView = (Bar2DVerView) this.findViewById(R.id.bar2DVerView);
		bar2DVerView.setOnTouchListener(this);
		bar2DVerView.setLongClickable(true);
		bar2DVerView.setSaveEnabled(false);
		
		bar2DVerView.setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				recoverBrightness();
				detector.onTouchEvent(arg1);
				return false;
			}
		});
		
		bar2DVerView.setBaseBean(baseBean);
		bar2DVerView.setPage(1);
		bar2DVerView.setScreenSize(screenSize);
		
		detector = new GestureDetector((OnGestureListener) this);

	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		super.onTouch(v,event);
		return detector.onTouchEvent(event);
	}

/*	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}*/

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		super.onFling(e1,e2,velocityX,velocityY);
		
		Log.i(LOG_TAG, "onFling()...");
		int currentPage = bar2DVerView.getPage();
		
        if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
        	Log.i(LOG_TAG,"Move Left");
        	++currentPage;
        	if(currentPage<=baseBean.getTotalPages()){
        		bar2DVerView.setPage(currentPage);
        		bar2DVerView.drawChart(bar2DVerView.getHolder(), baseBean, currentPage, screenSize);
        	}
        } else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
        	Log.i(LOG_TAG,"Move Right");
        	--currentPage;
        	if(currentPage>0){
        		bar2DVerView.setPage(currentPage);
        		bar2DVerView.drawChart(bar2DVerView.getHolder(), baseBean, currentPage, screenSize);
        	}
        }
		return false;
	}

	@Override
	public void changePriority(int priority) {
		baseBean = baseBeanList.get(priority);
		bar2DVerView.drawChart(bar2DVerView.getHolder(), baseBean, 1, screenSize);
	}

/*	@Override
	public void loadNextButton() {
		Button nextBtn = (Button) this.findViewById(R.id.nextBtn);
		nextBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				sendNextQuestion();
			}
		});
	}*/




	
}
